var commonAPI=require('../api/Common')
var token=wx.getStorageSync('token')
module.exports=
  {
    getToken:async function( code, nickName,avatar) {
      var _this=this
     await wx.cloud.callContainer({
     "config": {
       "env": "prod-3gfrot0nb41198bc"
     },
     "path": "/api/user/login",
     "header": {
       "X-WX-SERVICE": "springboot-zd5r",
       "content-type": "application/json"
     },
     "method": "POST",
     "data": {
       "code": code,
      //  "nickname": _this.data.nickName,
        "nickname": nickName,
       "avatar": avatar,
     }
   }).then((res)=>{
     wx.setStorageSync('token',res.data.result.token)
        // userId缓存
  
   })
   var res=await _this.getUserInfo()
   wx.setStorageSync('userId', res.data.result.id)
    },
    searchUser: function (keywords,page,size) {
      wx.cloud.callContainer({
        "config": {
          "env": "prod-3gfrot0nb41198bc"
        },
        "path": '/api/user/'+keywords+'/'+page+'/'+size,
        "header": {
          "X-WX-SERVICE": "springboot-zd5r",
          "content-type": "application/json",
          "token": commonAPI.getTokenFromLocal()
        },
        "method": "GET",
        "data": {}
      })
    },
    getUserInfo:async function () {
      console.log(token)
      token=wx.getStorageSync('token')
      console.log(token)
  
    var res=  await wx.cloud.callContainer({
        "config": {
          "env": "prod-3gfrot0nb41198bc"
        },
        "path": "/api/user",
        "header": {
          "X-WX-SERVICE": "springboot-zd5r",
          "content-type": "application/json",
          "token": token
        },
        "method": "GET",
        "data": ""
      }).then((res)=>{return res})
      console.log(res)
      
      return res
    },
    updateUser:async function(nickname,iconurl){
      var user={
        "id": "",
        "openId": "",
        "createTime": "",
        "modifyTime": "",
        "avatar": "demo.png", //可修改
        "isAdmin": 0, 
        "nickname": ""//可修改
    }
      user.nickname=nickname
      user.avatar=iconurl
      var res=wx.cloud.callContainer({
        "config": {
          "env": "prod-3gfrot0nb41198bc"
        },
        "path": "/api/user",
        "header": {
          "X-WX-SERVICE": "springboot-zd5r",
          "content-type": "application/json",
          "token": token
        },
        "method": "PUT",
        "data": user
      }).then((res)=>{return res})
      return res
    },
    getConnectedUserInfo: async function(){
      token= wx.getStorageSync('token')
      var res=await wx.cloud.callContainer({
        "config": {
          "env": "prod-3gfrot0nb41198bc"
        },
        "path": "/api/user/connected",
        "header": {
          "X-WX-SERVICE": "springboot-zd5r",
          "content-type": "application/json",
          "token": token
        },
        "method": "GET",
        "data": ''
      }).then((res)=>{return res})
      return res
    }

  }
